/**
* Copyright 2012 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.client.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.Layout;


public interface IHasWidgets extends HasWidgets {

	public class Helper {

		public static Widget onCreate(Widget w) {
            if (w instanceof IWidgetFactory<?>)
                w = ((IWidgetFactory<?>)w).getImpl();
            if (w instanceof IOnCreateHandler)
            	((IOnCreateHandler)w).onCreate();
            return w;
        }

		public static void add(Layout parent, Widget child) {
            child = Helper.onCreate(child);
        	if (child instanceof Canvas)
    			parent.addMember((Canvas)child);
    		else
    			parent.addMember(child);
        }

		public static <T> T[] extendArray(T[] values, T newValue) {
			final List<T> rslt = new ArrayList<T>(Arrays.asList(values));
			rslt.add(newValue);
			return rslt.toArray(values);
		}

		public static <T> T[] extendArray(T[] values, Collection<T> newValues) {
			final List<T> rslt = new ArrayList<T>(Arrays.asList(values));
			rslt.addAll(newValues);
			return rslt.toArray(values);
		}

	}

}
